using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using SheLife.Domain.Interfaces.Repositories;

namespace SheLife.Application.Services
{
    /// <summary>
    /// 护肤记录服务实现
    /// </summary>
    public class SkincareRecordService : ISkincareRecordService
    {
        private readonly IUnitOfWork _unitOfWork;

        public SkincareRecordService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取护肤记录
        /// </summary>
        /// <param name="id">护肤记录ID</param>
        /// <returns>护肤记录实体</returns>
        public async Task<SkincareRecord?> GetSkincareRecordByIdAsync(Guid id)
        {
            return await _unitOfWork.SkincareRecordRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 根据用户ID获取护肤记录列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>护肤记录列表</returns>
        public async Task<IEnumerable<SkincareRecord>> GetSkincareRecordsByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.SkincareRecordRepository.GetByUserIdAsync(userId);
        }

        /// <summary>
        /// 根据用户ID和日期范围获取护肤记录列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>护肤记录列表</returns>
        public async Task<IEnumerable<SkincareRecord>> GetSkincareRecordsByUserIdAndDateRangeAsync(Guid userId, DateTime startDate, DateTime endDate)
        {
            return await _unitOfWork.SkincareRecordRepository.GetByUserIdAndDateRangeAsync(userId, startDate, endDate);
        }

        /// <summary>
        /// 根据用户ID和护肤类型获取护肤记录列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="skincareType">护肤类型</param>
        /// <returns>护肤记录列表</returns>
        public async Task<IEnumerable<SkincareRecord>> GetSkincareRecordsByUserIdAndSkincareTypeAsync(Guid userId, string skincareType)
        {
            return await _unitOfWork.SkincareRecordRepository.GetByUserIdAndSkincareTypeAsync(userId, skincareType);
        }

        /// <summary>
        /// 创建护肤记录
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="date">日期</param>
        /// <param name="skincareType">护肤类型</param>
        /// <param name="description">描述</param>
        /// <param name="notes">备注</param>
        /// <returns>护肤记录实体</returns>
        public async Task<SkincareRecord> CreateSkincareRecordAsync(Guid userId, DateTime date, string skincareType, string description, string? notes = null)
        {
            var skincareRecord = SkincareRecord.Create(userId, date, skincareType, description, notes);
            await _unitOfWork.SkincareRecordRepository.AddAsync(skincareRecord);
            await _unitOfWork.SaveChangesAsync();
            return skincareRecord;
        }

        /// <summary>
        /// 更新护肤记录
        /// </summary>
        /// <param name="id">护肤记录ID</param>
        /// <param name="date">日期</param>
        /// <param name="skincareType">护肤类型</param>
        /// <param name="description">描述</param>
        /// <param name="notes">备注</param>
        /// <returns>护肤记录实体</returns>
        public async Task<SkincareRecord?> UpdateSkincareRecordAsync(Guid id, DateTime? date, string? skincareType, string? description, string? notes = null)
        {
            var skincareRecord = await _unitOfWork.SkincareRecordRepository.GetByIdAsync(id);
            if (skincareRecord == null)
            {
                return null;
            }

            skincareRecord.Update(date, skincareType, description, notes);
            await _unitOfWork.SkincareRecordRepository.UpdateAsync(skincareRecord);
            await _unitOfWork.SaveChangesAsync();
            return skincareRecord;
        }

        /// <summary>
        /// 删除护肤记录
        /// </summary>
        /// <param name="id">护肤记录ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteSkincareRecordAsync(Guid id)
        {
            var skincareRecord = await _unitOfWork.SkincareRecordRepository.GetByIdAsync(id);
            if (skincareRecord == null)
            {
                return false;
            }

            await _unitOfWork.SkincareRecordRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }

        /// <summary>
        /// 获取护肤统计数据
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="days">统计天数</param>
        /// <returns>统计数据字典</returns>
        public async Task<Dictionary<string, object>> GetSkincareStatisticsAsync(Guid userId, int days = 7)
        {
            var endDate = DateTime.Today;
            var startDate = endDate.AddDays(-days);
            var skincareRecords = await _unitOfWork.SkincareRecordRepository.GetByUserIdAndDateRangeAsync(userId, startDate, endDate);

            var statistics = new Dictionary<string, object>();

            if (skincareRecords.Any())
            {
                // 总体统计
                statistics["TotalRecords"] = skincareRecords.Count();
                statistics["ConsistencyRate"] = Math.Round((double)skincareRecords.Count() / days * 100, 2);

                // 按护肤类型统计
                var recordsByType = skincareRecords.GroupBy(r => r.SkincareType);
                var typeStats = new Dictionary<string, object>();
                foreach (var group in recordsByType)
                {
                    typeStats[group.Key] = new
                    {
                        Count = group.Count(),
                        Percentage = Math.Round((double)group.Count() / skincareRecords.Count() * 100, 2)
                    };
                }
                statistics["ByType"] = typeStats;

                // 按日期统计
                var recordsByDate = skincareRecords.GroupBy(r => r.Date.Date);
                var dateStats = new Dictionary<string, object>();
                foreach (var group in recordsByDate)
                {
                    dateStats[group.Key.ToString("yyyy-MM-dd")] = group.Count();
                }
                statistics["ByDate"] = dateStats;
            }
            else
            {
                statistics["TotalRecords"] = 0;
                statistics["ConsistencyRate"] = 0;
                statistics["ByType"] = new Dictionary<string, object>();
                statistics["ByDate"] = new Dictionary<string, object>();
            }

            return statistics;
        }
    }

    /// <summary>
    /// 护肤产品服务实现
    /// </summary>
    public class SkincareProductService : ISkincareProductService
    {
        private readonly IUnitOfWork _unitOfWork;

        public SkincareProductService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取护肤产品
        /// </summary>
        /// <param name="id">护肤产品ID</param>
        /// <returns>护肤产品实体</returns>
        public async Task<SkincareProduct?> GetSkincareProductByIdAsync(Guid id)
        {
            return await _unitOfWork.SkincareProductRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 获取所有护肤产品
        /// </summary>
        /// <returns>护肤产品列表</returns>
        public async Task<IEnumerable<SkincareProduct>> GetAllSkincareProductsAsync()
        {
            return await _unitOfWork.SkincareProductRepository.GetAllAsync();
        }

        /// <summary>
        /// 根据品牌获取护肤产品列表
        /// </summary>
        /// <param name="brand">品牌</param>
        /// <returns>护肤产品列表</returns>
        public async Task<IEnumerable<SkincareProduct>> GetSkincareProductsByBrandAsync(string brand)
        {
            return await _unitOfWork.SkincareProductRepository.GetByBrandAsync(brand);
        }

        /// <summary>
        /// 根据分类获取护肤产品列表
        /// </summary>
        /// <param name="category">分类</param>
        /// <returns>护肤产品列表</returns>
        public async Task<IEnumerable<SkincareProduct>> GetSkincareProductsByCategoryAsync(string category)
        {
            return await _unitOfWork.SkincareProductRepository.GetByCategoryAsync(category);
        }

        /// <summary>
        /// 搜索护肤产品
        /// </summary>
        /// <param name="keyword">关键词</param>
        /// <returns>护肤产品列表</returns>
        public async Task<IEnumerable<SkincareProduct>> SearchSkincareProductsAsync(string keyword)
        {
            return await _unitOfWork.SkincareProductRepository.SearchByNameOrDescriptionAsync(keyword);
        }

        /// <summary>
        /// 创建护肤产品
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="brand">品牌</param>
        /// <param name="category">分类</param>
        /// <param name="description">描述</param>
        /// <param name="imageUrl">图片URL</param>
        /// <returns>护肤产品实体</returns>
        public async Task<SkincareProduct> CreateSkincareProductAsync(string name, string brand, string category, string description, string? imageUrl = null)
        {
            var skincareProduct = SkincareProduct.Create(name, brand, category, description, imageUrl);
            await _unitOfWork.SkincareProductRepository.AddAsync(skincareProduct);
            await _unitOfWork.SaveChangesAsync();
            return skincareProduct;
        }

        /// <summary>
        /// 更新护肤产品
        /// </summary>
        /// <param name="id">护肤产品ID</param>
        /// <param name="name">名称</param>
        /// <param name="brand">品牌</param>
        /// <param name="category">分类</param>
        /// <param name="description">描述</param>
        /// <param name="imageUrl">图片URL</param>
        /// <returns>护肤产品实体</returns>
        public async Task<SkincareProduct?> UpdateSkincareProductAsync(Guid id, string? name, string? brand, string? category, string? description, string? imageUrl = null)
        {
            var skincareProduct = await _unitOfWork.SkincareProductRepository.GetByIdAsync(id);
            if (skincareProduct == null)
            {
                return null;
            }

            skincareProduct.Update(name, brand, category, description, imageUrl);
            await _unitOfWork.SkincareProductRepository.UpdateAsync(skincareProduct);
            await _unitOfWork.SaveChangesAsync();
            return skincareProduct;
        }

        /// <summary>
        /// 删除护肤产品
        /// </summary>
        /// <param name="id">护肤产品ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteSkincareProductAsync(Guid id)
        {
            var skincareProduct = await _unitOfWork.SkincareProductRepository.GetByIdAsync(id);
            if (skincareProduct == null)
            {
                return false;
            }

            await _unitOfWork.SkincareProductRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }
    }

    /// <summary>
    /// 护肤产品记录服务实现
    /// </summary>
    public class SkincareProductRecordService : ISkincareProductRecordService
    {
        private readonly IUnitOfWork _unitOfWork;

        public SkincareProductRecordService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取护肤产品记录
        /// </summary>
        /// <param name="id">护肤产品记录ID</param>
        /// <returns>护肤产品记录实体</returns>
        public async Task<SkincareProductRecord?> GetSkincareProductRecordByIdAsync(Guid id)
        {
            return await _unitOfWork.SkincareProductRecordRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 根据护肤记录ID获取护肤产品记录列表
        /// </summary>
        /// <param name="skincareRecordId">护肤记录ID</param>
        /// <returns>护肤产品记录列表</returns>
        public async Task<IEnumerable<SkincareProductRecord>> GetSkincareProductRecordsBySkincareRecordIdAsync(Guid skincareRecordId)
        {
            return await _unitOfWork.SkincareProductRecordRepository.GetBySkincareRecordIdAsync(skincareRecordId);
        }

        /// <summary>
        /// 根据用户ID和日期范围获取护肤产品记录列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>护肤产品记录列表</returns>
        public async Task<IEnumerable<SkincareProductRecord>> GetSkincareProductRecordsByUserIdAndDateRangeAsync(Guid userId, DateTime startDate, DateTime endDate)
        {
            return await _unitOfWork.SkincareProductRecordRepository.GetByUserIdAndDateRangeAsync(userId, startDate, endDate);
        }

        /// <summary>
        /// 创建护肤产品记录
        /// </summary>
        /// <param name="skincareRecordId">护肤记录ID</param>
        /// <param name="productId">产品ID</param>
        /// <param name="amount">用量</param>
        /// <returns>护肤产品记录实体</returns>
        public async Task<SkincareProductRecord> CreateSkincareProductRecordAsync(Guid skincareRecordId, Guid productId, decimal amount)
        {
            var skincareProductRecord = SkincareProductRecord.Create(skincareRecordId, productId, amount);
            await _unitOfWork.SkincareProductRecordRepository.AddAsync(skincareProductRecord);
            await _unitOfWork.SaveChangesAsync();
            return skincareProductRecord;
        }

        /// <summary>
        /// 更新护肤产品记录
        /// </summary>
        /// <param name="id">护肤产品记录ID</param>
        /// <param name="amount">用量</param>
        /// <returns>护肤产品记录实体</returns>
        public async Task<SkincareProductRecord?> UpdateSkincareProductRecordAsync(Guid id, decimal? amount)
        {
            var skincareProductRecord = await _unitOfWork.SkincareProductRecordRepository.GetByIdAsync(id);
            if (skincareProductRecord == null)
            {
                return null;
            }

            skincareProductRecord.Update(amount);
            await _unitOfWork.SkincareProductRecordRepository.UpdateAsync(skincareProductRecord);
            await _unitOfWork.SaveChangesAsync();
            return skincareProductRecord;
        }

        /// <summary>
        /// 删除护肤产品记录
        /// </summary>
        /// <param name="id">护肤产品记录ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteSkincareProductRecordAsync(Guid id)
        {
            var skincareProductRecord = await _unitOfWork.SkincareProductRecordRepository.GetByIdAsync(id);
            if (skincareProductRecord == null)
            {
                return false;
            }

            await _unitOfWork.SkincareProductRecordRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }
    }

    /// <summary>
    /// 用户护肤产品服务实现
    /// </summary>
    public class UserSkincareProductService : IUserSkincareProductService
    {
        private readonly IUnitOfWork _unitOfWork;

        public UserSkincareProductService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取用户护肤产品
        /// </summary>
        /// <param name="id">用户护肤产品ID</param>
        /// <returns>用户护肤产品实体</returns>
        public async Task<UserSkincareProduct?> GetUserSkincareProductByIdAsync(Guid id)
        {
            return await _unitOfWork.UserSkincareProductRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 根据用户ID获取用户护肤产品列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>用户护肤产品列表</returns>
        public async Task<IEnumerable<UserSkincareProduct>> GetUserSkincareProductsByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.UserSkincareProductRepository.GetByUserIdAsync(userId);
        }

        /// <summary>
        /// 根据用户ID和产品ID获取用户护肤产品列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="productId">产品ID</param>
        /// <returns>用户护肤产品列表</returns>
        public async Task<IEnumerable<UserSkincareProduct>> GetUserSkincareProductsByUserIdAndProductIdAsync(Guid userId, Guid productId)
        {
            return await _unitOfWork.UserSkincareProductRepository.GetByUserIdAndProductIdAsync(userId, productId);
        }

        /// <summary>
        /// 创建用户护肤产品
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="productId">产品ID</param>
        /// <param name="purchaseDate">购买日期</param>
        /// <param name="expiryDate">过期日期</param>
        /// <param name="notes">备注</param>
        /// <returns>用户护肤产品实体</returns>
        public async Task<UserSkincareProduct> CreateUserSkincareProductAsync(Guid userId, Guid productId, DateTime purchaseDate, DateTime? expiryDate = null, string? notes = null)
        {
            var userSkincareProduct = UserSkincareProduct.Create(userId, productId, purchaseDate, expiryDate, notes);
            await _unitOfWork.UserSkincareProductRepository.AddAsync(userSkincareProduct);
            await _unitOfWork.SaveChangesAsync();
            return userSkincareProduct;
        }

        /// <summary>
        /// 更新用户护肤产品
        /// </summary>
        /// <param name="id">用户护肤产品ID</param>
        /// <param name="purchaseDate">购买日期</param>
        /// <param name="expiryDate">过期日期</param>
        /// <param name="notes">备注</param>
        /// <returns>用户护肤产品实体</returns>
        public async Task<UserSkincareProduct?> UpdateUserSkincareProductAsync(Guid id, DateTime? purchaseDate, DateTime? expiryDate, string? notes)
        {
            var userSkincareProduct = await _unitOfWork.UserSkincareProductRepository.GetByIdAsync(id);
            if (userSkincareProduct == null)
            {
                return null;
            }

            userSkincareProduct.Update(purchaseDate, expiryDate, notes);
            await _unitOfWork.UserSkincareProductRepository.UpdateAsync(userSkincareProduct);
            await _unitOfWork.SaveChangesAsync();
            return userSkincareProduct;
        }

        /// <summary>
        /// 删除用户护肤产品
        /// </summary>
        /// <param name="id">用户护肤产品ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteUserSkincareProductAsync(Guid id)
        {
            var userSkincareProduct = await _unitOfWork.UserSkincareProductRepository.GetByIdAsync(id);
            if (userSkincareProduct == null)
            {
                return false;
            }

            await _unitOfWork.UserSkincareProductRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }

        /// <summary>
        /// 获取即将过期的用户护肤产品
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="days">天数</param>
        /// <returns>用户护肤产品列表</returns>
        public async Task<IEnumerable<UserSkincareProduct>> GetExpiringUserSkincareProductsAsync(Guid userId, int days = 30)
        {
            var thresholdDate = DateTime.Today.AddDays(days);
            return await _unitOfWork.UserSkincareProductRepository.GetExpiringProductsAsync(userId, thresholdDate);
        }
    }

    /// <summary>
    /// 护肤提醒服务实现
    /// </summary>
    public class SkincareReminderService : ISkincareReminderService
    {
        private readonly IUnitOfWork _unitOfWork;

        public SkincareReminderService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取护肤提醒
        /// </summary>
        /// <param name="id">护肤提醒ID</param>
        /// <returns>护肤提醒实体</returns>
        public async Task<SkincareReminder?> GetSkincareReminderByIdAsync(Guid id)
        {
            return await _unitOfWork.SkincareReminderRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 根据用户ID获取护肤提醒列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>护肤提醒列表</returns>
        public async Task<IEnumerable<SkincareReminder>> GetSkincareRemindersByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.SkincareReminderRepository.GetByUserIdAsync(userId);
        }

        /// <summary>
        /// 根据用户ID和启用状态获取护肤提醒列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="isEnabled">是否启用</param>
        /// <returns>护肤提醒列表</returns>
        public async Task<IEnumerable<SkincareReminder>> GetSkincareRemindersByUserIdAndIsEnabledAsync(Guid userId, bool isEnabled)
        {
            return await _unitOfWork.SkincareReminderRepository.GetByUserIdAndIsEnabledAsync(userId, isEnabled);
        }

        /// <summary>
        /// 创建护肤提醒
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="title">标题</param>
        /// <param name="description">描述</param>
        /// <param name="reminderTime">提醒时间</param>
        /// <param name="repeatType">重复类型</param>
        /// <param name="repeatDays">重复天数</param>
        /// <returns>护肤提醒实体</returns>
        public async Task<SkincareReminder> CreateSkincareReminderAsync(Guid userId, string title, string description, TimeSpan reminderTime, string repeatType, List<int>? repeatDays = null)
        {
            var skincareReminder = SkincareReminder.Create(userId, title, description, reminderTime, repeatType, repeatDays);
            await _unitOfWork.SkincareReminderRepository.AddAsync(skincareReminder);
            await _unitOfWork.SaveChangesAsync();
            return skincareReminder;
        }

        /// <summary>
        /// 更新护肤提醒
        /// </summary>
        /// <param name="id">护肤提醒ID</param>
        /// <param name="title">标题</param>
        /// <param name="description">描述</param>
        /// <param name="reminderTime">提醒时间</param>
        /// <param name="repeatType">重复类型</param>
        /// <param name="repeatDays">重复天数</param>
        /// <param name="isEnabled">是否启用</param>
        /// <returns>护肤提醒实体</returns>
        public async Task<SkincareReminder?> UpdateSkincareReminderAsync(Guid id, string? title, string? description, TimeSpan? reminderTime, string? repeatType, List<int>? repeatDays, bool? isEnabled)
        {
            var skincareReminder = await _unitOfWork.SkincareReminderRepository.GetByIdAsync(id);
            if (skincareReminder == null)
            {
                return null;
            }

            skincareReminder.Update(title, description, reminderTime, repeatType, repeatDays, isEnabled);
            await _unitOfWork.SkincareReminderRepository.UpdateAsync(skincareReminder);
            await _unitOfWork.SaveChangesAsync();
            return skincareReminder;
        }

        /// <summary>
        /// 删除护肤提醒
        /// </summary>
        /// <param name="id">护肤提醒ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteSkincareReminderAsync(Guid id)
        {
            var skincareReminder = await _unitOfWork.SkincareReminderRepository.GetByIdAsync(id);
            if (skincareReminder == null)
            {
                return false;
            }

            await _unitOfWork.SkincareReminderRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }

        /// <summary>
        /// 启用护肤提醒
        /// </summary>
        /// <param name="id">护肤提醒ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> EnableSkincareReminderAsync(Guid id)
        {
            var skincareReminder = await _unitOfWork.SkincareReminderRepository.GetByIdAsync(id);
            if (skincareReminder == null)
            {
                return false;
            }

            skincareReminder.Enable();
            await _unitOfWork.SkincareReminderRepository.UpdateAsync(skincareReminder);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }

        /// <summary>
        /// 禁用护肤提醒
        /// </summary>
        /// <param name="id">护肤提醒ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DisableSkincareReminderAsync(Guid id)
        {
            var skincareReminder = await _unitOfWork.SkincareReminderRepository.GetByIdAsync(id);
            if (skincareReminder == null)
            {
                return false;
            }

            skincareReminder.Disable();
            await _unitOfWork.SkincareReminderRepository.UpdateAsync(skincareReminder);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }
    }

    /// <summary>
    /// 护肤计划服务实现
    /// </summary>
    public class SkincarePlanService : ISkincarePlanService
    {
        private readonly IUnitOfWork _unitOfWork;

        public SkincarePlanService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取护肤计划
        /// </summary>
        /// <param name="id">护肤计划ID</param>
        /// <returns>护肤计划实体</returns>
        public async Task<SkincarePlan?> GetSkincarePlanByIdAsync(Guid id)
        {
            return await _unitOfWork.SkincarePlanRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 获取所有护肤计划
        /// </summary>
        /// <returns>护肤计划列表</returns>
        public async Task<IEnumerable<SkincarePlan>> GetAllSkincarePlansAsync()
        {
            return await _unitOfWork.SkincarePlanRepository.GetAllAsync();
        }

        /// <summary>
        /// 根据分类获取护肤计划列表
        /// </summary>
        /// <param name="category">分类</param>
        /// <returns>护肤计划列表</returns>
        public async Task<IEnumerable<SkincarePlan>> GetSkincarePlansByCategoryAsync(string category)
        {
            return await _unitOfWork.SkincarePlanRepository.GetByCategoryAsync(category);
        }

        /// <summary>
        /// 根据肤质获取护肤计划列表
        /// </summary>
        /// <param name="skinType">肤质</param>
        /// <returns>护肤计划列表</returns>
        public async Task<IEnumerable<SkincarePlan>> GetSkincarePlansBySkinTypeAsync(string skinType)
        {
            return await _unitOfWork.SkincarePlanRepository.GetBySkinTypeAsync(skinType);
        }

        /// <summary>
        /// 搜索护肤计划
        /// </summary>
        /// <param name="keyword">关键词</param>
        /// <returns>护肤计划列表</returns>
        public async Task<IEnumerable<SkincarePlan>> SearchSkincarePlansAsync(string keyword)
        {
            return await _unitOfWork.SkincarePlanRepository.SearchByNameOrDescriptionAsync(keyword);
        }

        /// <summary>
        /// 创建护肤计划
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="description">描述</param>
        /// <param name="category">分类</param>
        /// <param name="skinType">肤质</param>
        /// <param name="imageUrl">图片URL</param>
        /// <returns>护肤计划实体</returns>
        public async Task<SkincarePlan> CreateSkincarePlanAsync(string name, string description, string category, string skinType, string? imageUrl = null)
        {
            var skincarePlan = SkincarePlan.Create(name, description, category, skinType, imageUrl);
            await _unitOfWork.SkincarePlanRepository.AddAsync(skincarePlan);
            await _unitOfWork.SaveChangesAsync();
            return skincarePlan;
        }

        /// <summary>
        /// 更新护肤计划
        /// </summary>
        /// <param name="id">护肤计划ID</param>
        /// <param name="name">名称</param>
        /// <param name="description">描述</param>
        /// <param name="category">分类</param>
        /// <param name="skinType">肤质</param>
        /// <param name="imageUrl">图片URL</param>
        /// <returns>护肤计划实体</returns>
        public async Task<SkincarePlan?> UpdateSkincarePlanAsync(Guid id, string? name, string? description, string? category, string? skinType, string? imageUrl = null)
        {
            var skincarePlan = await _unitOfWork.SkincarePlanRepository.GetByIdAsync(id);
            if (skincarePlan == null)
            {
                return null;
            }

            skincarePlan.Update(name, description, category, skinType, imageUrl);
            await _unitOfWork.SkincarePlanRepository.UpdateAsync(skincarePlan);
            await _unitOfWork.SaveChangesAsync();
            return skincarePlan;
        }

        /// <summary>
        /// 删除护肤计划
        /// </summary>
        /// <param name="id">护肤计划ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteSkincarePlanAsync(Guid id)
        {
            var skincarePlan = await _unitOfWork.SkincarePlanRepository.GetByIdAsync(id);
            if (skincarePlan == null)
            {
                return false;
            }

            await _unitOfWork.SkincarePlanRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }
    }

    /// <summary>
    /// 护肤计划步骤服务实现
    /// </summary>
    public class SkincarePlanStepService : ISkincarePlanStepService
    {
        private readonly IUnitOfWork _unitOfWork;

        public SkincarePlanStepService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取护肤计划步骤
        /// </summary>
        /// <param name="id">护肤计划步骤ID</param>
        /// <returns>护肤计划步骤实体</returns>
        public async Task<SkincarePlanStep?> GetSkincarePlanStepByIdAsync(Guid id)
        {
            return await _unitOfWork.SkincarePlanStepRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 根据护肤计划ID获取护肤计划步骤列表
        /// </summary>
        /// <param name="planId">护肤计划ID</param>
        /// <returns>护肤计划步骤列表</returns>
        public async Task<IEnumerable<SkincarePlanStep>> GetSkincarePlanStepsByPlanIdAsync(Guid planId)
        {
            return await _unitOfWork.SkincarePlanStepRepository.GetByPlanIdAsync(planId);
        }

        /// <summary>
        /// 创建护肤计划步骤
        /// </summary>
        /// <param name="planId">护肤计划ID</param>
        /// <param name="stepNumber">步骤编号</param>
        /// <param name="title">标题</param>
        /// <param name="description">描述</param>
        /// <param name="timeOfDay">时间点</param>
        /// <param name="imageUrl">图片URL</param>
        /// <returns>护肤计划步骤实体</returns>
        public async Task<SkincarePlanStep> CreateSkincarePlanStepAsync(Guid planId, int stepNumber, string title, string description, string timeOfDay, string? imageUrl = null)
        {
            var skincarePlanStep = SkincarePlanStep.Create(planId, stepNumber, title, description, timeOfDay, imageUrl);
            await _unitOfWork.SkincarePlanStepRepository.AddAsync(skincarePlanStep);
            await _unitOfWork.SaveChangesAsync();
            return skincarePlanStep;
        }

        /// <summary>
        /// 更新护肤计划步骤
        /// </summary>
        /// <param name="id">护肤计划步骤ID</param>
        /// <param name="stepNumber">步骤编号</param>
        /// <param name="title">标题</param>
        /// <param name="description">描述</param>
        /// <param name="timeOfDay">时间点</param>
        /// <param name="imageUrl">图片URL</param>
        /// <returns>护肤计划步骤实体</returns>
        public async Task<SkincarePlanStep?> UpdateSkincarePlanStepAsync(Guid id, int? stepNumber, string? title, string? description, string? timeOfDay, string? imageUrl = null)
        {
            var skincarePlanStep = await _unitOfWork.SkincarePlanStepRepository.GetByIdAsync(id);
            if (skincarePlanStep == null)
            {
                return null;
            }

            skincarePlanStep.Update(stepNumber, title, description, timeOfDay, imageUrl);
            await _unitOfWork.SkincarePlanStepRepository.UpdateAsync(skincarePlanStep);
            await _unitOfWork.SaveChangesAsync();
            return skincarePlanStep;
        }

        /// <summary>
        /// 删除护肤计划步骤
        /// </summary>
        /// <param name="id">护肤计划步骤ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteSkincarePlanStepAsync(Guid id)
        {
            var skincarePlanStep = await _unitOfWork.SkincarePlanStepRepository.GetByIdAsync(id);
            if (skincarePlanStep == null)
            {
                return false;
            }

            await _unitOfWork.SkincarePlanStepRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }
    }

    /// <summary>
    /// 用户护肤计划服务实现
    /// </summary>
    public class UserSkincarePlanService : IUserSkincarePlanService
    {
        private readonly IUnitOfWork _unitOfWork;

        public UserSkincarePlanService(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 根据ID获取用户护肤计划
        /// </summary>
        /// <param name="id">用户护肤计划ID</param>
        /// <returns>用户护肤计划实体</returns>
        public async Task<UserSkincarePlan?> GetUserSkincarePlanByIdAsync(Guid id)
        {
            return await _unitOfWork.UserSkincarePlanRepository.GetByIdAsync(id);
        }

        /// <summary>
        /// 根据用户ID获取用户护肤计划列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>用户护肤计划列表</returns>
        public async Task<IEnumerable<UserSkincarePlan>> GetUserSkincarePlansByUserIdAsync(Guid userId)
        {
            return await _unitOfWork.UserSkincarePlanRepository.GetByUserIdAsync(userId);
        }

        /// <summary>
        /// 根据用户ID和计划ID获取用户护肤计划列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="planId">计划ID</param>
        /// <returns>用户护肤计划列表</returns>
        public async Task<IEnumerable<UserSkincarePlan>> GetUserSkincarePlansByUserIdAndPlanIdAsync(Guid userId, Guid planId)
        {
            return await _unitOfWork.UserSkincarePlanRepository.GetByUserIdAndPlanIdAsync(userId, planId);
        }

        /// <summary>
        /// 创建用户护肤计划
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="planId">计划ID</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="notes">备注</param>
        /// <returns>用户护肤计划实体</returns>
        public async Task<UserSkincarePlan> CreateUserSkincarePlanAsync(Guid userId, Guid planId, DateTime startDate, string? notes = null)
        {
            var userSkincarePlan = UserSkincarePlan.Create(userId, planId, startDate, notes);
            await _unitOfWork.UserSkincarePlanRepository.AddAsync(userSkincarePlan);
            await _unitOfWork.SaveChangesAsync();
            return userSkincarePlan;
        }

        /// <summary>
        /// 更新用户护肤计划
        /// </summary>
        /// <param name="id">用户护肤计划ID</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="notes">备注</param>
        /// <returns>用户护肤计划实体</returns>
        public async Task<UserSkincarePlan?> UpdateUserSkincarePlanAsync(Guid id, DateTime? startDate, DateTime? endDate, string? notes)
        {
            var userSkincarePlan = await _unitOfWork.UserSkincarePlanRepository.GetByIdAsync(id);
            if (userSkincarePlan == null)
            {
                return null;
            }

            userSkincarePlan.Update(startDate, endDate, notes);
            await _unitOfWork.UserSkincarePlanRepository.UpdateAsync(userSkincarePlan);
            await _unitOfWork.SaveChangesAsync();
            return userSkincarePlan;
        }

        /// <summary>
        /// 删除用户护肤计划
        /// </summary>
        /// <param name="id">用户护肤计划ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> DeleteUserSkincarePlanAsync(Guid id)
        {
            var userSkincarePlan = await _unitOfWork.UserSkincarePlanRepository.GetByIdAsync(id);
            if (userSkincarePlan == null)
            {
                return false;
            }

            await _unitOfWork.UserSkincarePlanRepository.DeleteAsync(id);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }

        /// <summary>
        /// 完成用户护肤计划
        /// </summary>
        /// <param name="id">用户护肤计划ID</param>
        /// <returns>是否成功</returns>
        public async Task<bool> CompleteUserSkincarePlanAsync(Guid id)
        {
            var userSkincarePlan = await _unitOfWork.UserSkincarePlanRepository.GetByIdAsync(id);
            if (userSkincarePlan == null)
            {
                return false;
            }

            userSkincarePlan.Complete();
            await _unitOfWork.UserSkincarePlanRepository.UpdateAsync(userSkincarePlan);
            await _unitOfWork.SaveChangesAsync();
            return true;
        }
    }
}